package model.turtle;

import java.util.LinkedList;
import java.util.List;

/**
 * The turtle class. myDirection is an non-negative integer smaller or equal to
 * 360.
 */
public class Turtle
{
	private List<Position> trail;
	private Position position;
	private boolean penDown;
	private boolean show;
	private String myCommand;
	
	public void readCommand(String input)
	{
		myCommand = input;
	}

	/**
	 * @return the penDown
	 */
	public boolean isPenDown()
	{
		return penDown;
	}

	/**
	 * @param penDown
	 *            the penDown to set
	 */
	public void setPenDown(boolean penDown)
	{
		this.penDown = penDown;
	}

	/**
	 * @return the show
	 */
	public boolean isShow()
	{
		return show;
	}

	/**
	 * @param show
	 *            the show to set
	 */
	public void setShow(boolean show)
	{
		this.show = show;
	}

	/**
	 * @return the myDirection
	 */
	public int getDirection()
	{
		return position.getDirection();
	}

	/**
	 * @param myDirection
	 *            the myDirection to set
	 */
	public void setDirection(int myDirection)
	{
		position.setDirection(myDirection);
	}

	/**
	 * @return the position of the turtle.
	 */
	public Position getPosition() {
		return position;
	}
	
	/**
	 * Build a new turtle.
	 * 
	 * @param x
	 * @param y
	 * @param direction
	 */
	public Turtle(int x, int y, int direction)
	{
		this(new Position(x, y, direction));
	}
	
	public Turtle(Position pos) {
		position = new Position(pos);
		trail = new LinkedList<Position>();
		trail.add(pos);

		show = true;
		penDown = true;
	}

	/**
	 * Get the list of trail.
	 * 
	 * @return
	 */
	public List<Position> getTrail()
	{
		return trail;
	}

	public void addTrail(int x, int y, int d)
	{
		trail.add(new Position(x, y, d % 360));
	}

	public void fd(int d)
	{
		position.x = (int) (position.x + Math.cos(position.getDirection() * Math.PI / 180) * d);
		position.y = (int) (position.y + Math.sin(position.getDirection() * Math.PI / 180) * d);
		buildTrail();
	}

	public void bk(int d)
	{
		fd(-d);
		reverseDirection();
	}

	public void reverseDirection()
	{
		if (position.getDirection() >= 180)
			position.setDirection(position.getDirection() - 180);
		else
			position.setDirection(360 + (position.getDirection() - 180));
	}

	public void rt(int d)
	{
		lt(-d);
	}

	public void lt(int d)
	{
		position.setDirection(position.getDirection() + d);
		buildTrail();
	}

	public void buildTrail()
	{
		trail.add(new Position(position.x,  position.y, position.getDirection()));
	}

	public void home()
	{
		position.x = 0;
		position.y = 0;
		position.setDirection(90);
		trail.clear();
		buildTrail();
	}

	public void setXY(Integer x, Integer y) {
		position.x = x;
		position.y = y;
	}
}
